<template>
  <div id="app" class="app-container">
    <!-- 头部控制栏 -->
    <div class="control-bar">
      <h1><i class="el-icon-date"></i> 本地日程</h1>
      <div class="header-actions">
        <el-radio-group 
          v-model="currentView" 
          size="small"
          class="compact-view-switcher"
        >
          <el-radio-button label="week">周视图</el-radio-button>
          <el-radio-button label="month">月视图</el-radio-button>
        </el-radio-group>
        <el-button 
          class="create-btn"
          type="primary" 
          size="small"
          icon="el-icon-plus"
          @click="openForm"
        >
          新建日程
        </el-button>
      </div>
    </div>

    <!-- 日程表单 -->
    <ScheduleForm
      :visible.sync="showForm"
      :schedule="editingSchedule"
      @submit="handleSubmit"
    />

    <!-- 视图容器 -->
    <div class="calendar-container">
      <CalendarWeek
        v-show="currentView === 'week'"
        :schedules="schedules"
        @edit="openEditForm"
        @delete="deleteSchedule"
      />
      <CalendarMonth
        v-show="currentView === 'month'"
        :schedules="schedules"
        @edit="openEditForm"
        @delete="deleteSchedule"
        @day-click="handleDayClick"
      />
    </div>
  </div>
</template>

<script>
import ScheduleForm from '@/components/Schedule/ScheduleForm.vue'
import CalendarWeek from '@/components/Schedule/CalendarWeek.vue'
import CalendarMonth from '@/components/Schedule/CalendarMonth.vue'

export default {
  components: { ScheduleForm, CalendarWeek, CalendarMonth },
  data() {
    return {
      currentView: 'month',
      showForm: false,
      editingSchedule: null,
      schedules: JSON.parse(localStorage.getItem('schedules')) || []
    }
  },
  methods: {
    openForm() {
      this.editingSchedule = {
        date: new Date().toISOString().split('T')[0],
        startHour: 9,
        endHour: 10,
        title: '',
        id: null
      }
      this.showForm = true
    },
    openEditForm(schedule) {
      this.editingSchedule = { ...schedule }
      this.showForm = true
    },
    handleSubmit(schedule) {
      if (schedule.id) {
        const index = this.schedules.findIndex(s => s.id === schedule.id)
        this.schedules.splice(index, 1, schedule)
      } else {
        schedule.id = Date.now()
        this.schedules.push(schedule)
      }
      this.saveData()
    },
    deleteSchedule(id) {
      this.$confirm('确定删除该日程吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.schedules = this.schedules.filter(s => s.id !== id)
        this.saveData()
      })
    },
    handleDayClick(date) {
      this.editingSchedule = {
        date,
        startHour: 9,
        endHour: 10,
        title: '',
        id: null
      }
      this.showForm = true
    },
    saveData() {
      localStorage.setItem('schedules', JSON.stringify(this.schedules))
      console.log(this.schedules);
    }
  }
}
</script>

<style scoped>
.app-container {
  max-width: 100%;
  padding: 10px;
}

/* 头部控制栏样式 */
.control-bar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}

.header-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

/* 紧凑的视图切换器 */
.compact-view-switcher {
  margin-right: 12px;
}
.compact-view-switcher .el-radio-button:first-child .el-radio-button__inner {
  border-right: none;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.compact-view-switcher .el-radio-button:last-child .el-radio-button__inner {
  border-left: none;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

/* 日历容器 */
.calendar-container {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 20px;
}

/* 移动端适配 */
@media (max-width: 767px) {
  .header-actions {
    flex-direction: row;
  }
  
  .calendar-container {
    padding: 10px 0;
    box-shadow: none;
  }
}

/* 电脑端适配 */
@media (min-width: 768px) {
  .control-bar {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .control-bar h1 {
    margin-bottom: 15px;
  }
  
  .header-actions {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .compact-view-switcher {
    margin-right: 0;
  }
  
  .create-btn {
    margin-left: auto;
  }
}
</style>